<?php
namespace Admin\Controller;
use Org\Util\ArrayList;
use Think\Controller;
class OrderController extends Controller {
    public function showOrder(){
        $order= M('order')->select();
        $pagesize = 5;#每页数量
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $count = count($order);
        $offset = $pagesize * ($p - 1);//计算记录偏移量

        $dish= M('order')
            ->limit($offset . ',' . $pagesize)
            ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('page', $page);

        $this->assign('order', $dish);
        $this->display();
    }
    public function checkout(){
        $tableNum = I('tableNum');
        $table_num = $tableNum;
        $orderId = I('orderId');
        if ( strstr($tableNum,',')){
            $tableNum =explode(',',$tableNum);
        }
        $data['table_num']=$table_num;
        $data['order_id']=$orderId;
        $order = M('order');
        $data2['status']=1;
        $data2['tableNum']=$tableNum;
        $data1['order_status'] = 1;
        if ($order->where($data)->save($data1)){
            $table = M('table');
            $tableInfo['table_status'] =0;
            if (strstr($table_num,',')){
                foreach ($tableNum as $table_info){
                    $table->where('table_num='.$table_info)->save($tableInfo);
                }
            }else{
                $table->where('table_num='.$table_num)->save($tableInfo);
            }
            $data['status']= 0;
            show(30001, "确认订单成功：第".$table_num."桌订单完成", $data2);
        }else{
            show(31001, "确认订单失败：第".$table_num."桌空闲", $data2);
        }
        show(32001, "请求失败");

    }


    public function addOrder(){
        $tableNum = $_POST['tableNum'];
        $table_num = $tableNum;
        if (is_array($tableNum)){
            $tableNum =implode(',',$tableNum);
        }

        $userName = $_POST['userName'];
        $userContact = $_POST['userContact'];
        $danhao = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $dishs = $_POST['dishs'];
//        var_dump($dishs);
//        $jsonArr = json_decode($dishs, TRUE);
        $order_item = M("order_item"); // 实例化order对象
        $totalprice=0;
        $item_count=0;

//        echo count($dishs);
        for ($i= 0;$i< count($dishs); $i++){
            $data['order_id']=$danhao;
            $data['item_id']=$dishs[$i]['itemId'];
            $data['item_name']=$dishs[$i]['name'];
            $data['item_price']=$dishs[$i]['price'];
            $data['item_count']=$dishs[$i]['count'];
            $data['create_time'] = date("Y-m-d H:i:s");// 当前时间年月日 时分秒;
            $data['update_time'] = date("Y-m-d H:i:s");// 当前时间年月日 时分秒;
            $totalprice+=$dishs[$i]['price']*$dishs[$i]['count'];
            $item_count+=$dishs[$i]['count'];
            $order_item->add($data);
        }

        $order = M("Order"); // 实例化order对象
        $data['order_id']=$danhao;
        $data['table_num'] = $tableNum;
        $data['user_name'] = $userName;
        $data['user_contact'] = $userContact;
        $data['price'] = $totalprice;
        $data['item_count'] = $item_count;
        $data['order_status'] = 1;
        $data['order_status'] = 0;

        $data['create_time'] = date("Y-m-d H:i:s");// 当前时间年月日 时分秒;
        $data['update_time'] = date("Y-m-d H:i:s");// 当前时间年月日 时分秒;
        $order->add($data);
        $table = M('table');
        $tableInfo['table_status'] =1;
        if (is_array($table_num)){
            foreach ($table_num as $table_info){
                $table->where('table_num='.$table_info)->save($tableInfo);

            }
        }else{
            $table->where('table_num='.$tableNum)->save($tableInfo);

        }
        $result['orderId']=$danhao;
        $result['code']=22001;
        echo json_encode($result);
       /*
               $table = M('table');
         if ($table->find($tableNum)['table_status']!=0){
            $data['tableNum']=1;
            $data['status']=0;
            show(31001, '确认订单失败：第'.$tableNum.'桌空闲',$data);
        }
       if($order->add($data)){
           $data['tableNum']=1;
           $data['status']=0;
           show(31001, '确认订单失败：第'.$tableNum.'桌空闲',$data);

       } ;
        $data['tableNum']=1;
        $data['status']=0;
        show(30001, '确认订单成功：第1桌订单完成',$data);
       /* $result["orderId"]=$danhao;
        $result["status"]=22001;
//        var_dump($result);
        echo json_encode($result);*/
    }

    public function getOrderList(){
/*        $order= M('order')->query('SELECT * from wm_order ORDER BY create_time DESC');*/
        $order= M('order')->query('SELECT * from wm_order WHERE order_status = 0 ORDER BY create_time DESC;');
        $a = array();
        for ($i= 0;$i< count($order); $i++){
            $data['tableNum']=$order[$i]['table_num'];
            $data['orderId']=$order[$i]['order_id'];
            $data['name']="桌号".$order[$i]['table_num'];
            $data['count']=$order[$i]['item_count'];
            $data['price']=$order[$i]['price'];
            $data['orderStatus']=$order[$i]['order_status'];
            $data['create_time']=$order[$i]['create_time'];


            $order_item= M('order_item')->where('order_id='.$order[$i]['order_id'])->select();
            $dishs= M('dish')->select();
            $b = array();
            foreach ($order_item as $dishInfo){
                $dish = new \stdClass();
                $dish->itemId = $dishInfo['item_id'];
                $dish->name = $dishInfo['item_name'];
                $dish->count = $dishInfo['item_count'];
                $dish->price = $dishInfo['item_price'];
                array_push($b,$dish);
            }

            $data['dish']=$b;
            array_push($a,$data);
        }
        echo json_encode($a);
    }
}